#!/bin/bash
#set -n
#set -x
lstart_secs=$SECONDS
is_cygwin=$1
is_vista=$2
tmpb=$II/tmpb$$ #.wmf$' -e 'Root$' -e 'Tag$' -e '\.dfUseCase$' -e 'class'
function common_lines
{
#set -x
fn=$1
tag=$2
adir=$3
ic=0

#Using ant.bat is faster on windows (I think other ant may access login files).
if (( is_cygwin == 1 )) ; then
  ant_pgm=$I/antl.sh
else
  ant_pgm=$I/antl.sh
fi
adir_base=$(basename $adir)
echo "export ${adir_base}_file=$fn
export ${adir_base}_tag=$tag" >> $ii/srcenv.sh
rm -f $ii/$fn
#set -x
while (( ic < ${#vfiles[*]} )) ; do
  echo ":e \$II/${vfiles[$ic]}" >> $ii/$fn
  (( ic = ic + 1 ))
done
echo ":e \$II/$fn.logcvs" >> $ii/$fn
echo ":e \$II/$fn.cotcvs" >> $ii/$fn
set -x
if [[ (-d $II/$adir) || (-L $II/$adir) ]] ; then
	sdir=$II/$adir
elif [[ (-d $adir) || (-L $adir) ]] ; then
	sdir=$adir
else
	echo "$II/$adir and $adir not valid directories" >&2
	return 1
fi
forums_make_parms="-DJAMES_BUILD_FOR=forums\ -DJAMES_BUILD_EXCLUDE=itrc"
forums_other_bld_parms="-DJAMES_BUILD_FOR=forums\ -DJAMES_BUILD_EXCLUDE=itrc"
if [[ "$fn" = "allfilesja" ]] ; then
	make_bld_parms='-f\ build_newssa.xml'
	other_bld_parms='-f build_newssa.xml'
	ant_dir="$sdir/com/hp/james"
elif [[ "$fn" = "allfileswja" ]] ; then
	make_bld_parms='-f\ build_newwpa.xml'
	other_bld_parms='-f build_newwpa.xml'
	ant_dir="$ja/com/hp/james"
else
	make_bld_parms=''
	other_bld_parms=''
	ant_dir="$sdir"
fi
if [[ -z "$make_bld_parms" ]] ; then
	alt_make_bld_parms=
	alt_other_bld_parms=
else
	alt_make_bld_parms="$make_bld_parms\ $forums_make_parms"
	alt_other_bld_parms="$other_bld_parms $forums_other_bld_parms"
fi
set +x
csout="\$II/$fn.cscope.out"
printf ":map <F4> :e \$II/${vfiles[4]} | map <S-F4> :e \$II/${vfiles[5]} | map <F6> :e \$II/${vfiles[0]} | map <F7> :e \$II/${vfiles[1]} | map <S-F11> :e \$II/${vfiles[2]} | map <S-F12> :e \$II/${vfiles[3]} | map <S-F10> :e \$II/allfileswki\r\n" >> $ii/$fn
printf ":map <F4> :e \$II/${fn}\r\n" >> $ii/$fn
printf ":map <S-F4> :e \$II/${fn}\r\n" >> $ii/$fn
printf ":map <F6> :e \$II/${fn}\r\n" >> $ii/$fn
printf ":map <F7> :e \$II/${fn}\r\n" >> $ii/$fn
printf ":map <S-F11> :e \$II/${fn}\r\n" >> $ii/$fn
printf ":map <S-F12> :e \$II/${fn}\r\n" >> $ii/$fn
printf ":map <S-F10> :e \$II/${fn}\r\n" >> $ii/$fn
echo "" >> $ii/$fn
echo ":'b,'e s/[^ ][^ ]* /:!\$I\/diffcvs.sh /" >> $ii/$fn
echo ":'b,'e s/[^ ][^ ]* /:!\$I\/commitcvs.sh /" >> $ii/$fn
echo ":'b,'e s/[^ ][^ ]* /:!\$I\/updatecvs.sh /" >> $ii/$fn
echo ":'b,'e s/[^ ][^ ]* /:!\$I\/addcvs.sh /" >> $ii/$fn
echo ":'b,'e s/[^ ][^ ]* /:!\$I\/removecvs.sh /" >> $ii/$fn
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ $make_bld_parms\ compile" >> $ii/$fn
if [[ -n "$make_bld_parms" ]] ; then
	echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ $alt_make_bld_parms\ compile" >> $ii/$fn
fi
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ compile-tests" >> $ii/$fn
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ -Dcompile.done=true\ iutest.compile" >> $ii/$fn
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ iutest.prepare" >> $ii/$fn
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ test.compile" >> $ii/$fn
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ compile-tests" >> $ii/$fn
echo ":set makeprg=cd\ $ant_dir;$ant_pgm\ junitdoclet.compile\ -Djunitdoclet.generated=true" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.java'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.jsp'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.properties'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.tld'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.xml'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.css'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.html'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.sh'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.pm'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.pl'\\ -n" >> $ii/$fn
echo ":set grepprg=\$I/grepf.ksh\\ $sdir\\ '*.*'\\ -n" >> $ii/$fn
echo ":!cd $sdir;/usr/local/bin/cscope -b  -f $csout -i $II/$fn.cscope" >> $ii/$fn
echo ":cs add $csout" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm $other_bld_parms deploy 2>&1 | /bin/tee \$II/whydep" >> $ii/$fn
if [[ -n "$make_bld_parms" ]] ; then
	echo ":!cd $ant_dir;$ant_pgm $alt_other_bld_parms deploy 2>&1 | /bin/tee \$II/whydep" >> $ii/$fn
fi
echo ":!cd $ant_dir;$ant_pgm $other_bld_parms clean" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm $other_bld_parms clean_classes" >> $ii/$fn
if [[ -n "$make_bld_parms" ]] ; then
	echo ":!cd $ant_dir;$ant_pgm $alt_other_bld_parms clean" >> $ii/$fn
	echo ":!cd $ant_dir;$ant_pgm $alt_other_bld_parms clean_classes" >> $ii/$fn
fi
echo ":!cd $ant_dir;$ant_pgm $other_bld_parms clean_all" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm $other_bld_parms clean_deploy" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm test.compile" >> $II/$fn
echo ":!cd $ant_dir;$ant_pgm iutest.compile" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm junitdoclet.compile" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm test 2>&1 | /bin/tee \$II/whytest" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm iutest 2>&1 | /bin/tee \$II/whytest" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm junitdoclet 2>&1 | /bin/tee \$II/whytest" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm junitdoclet -Djunitdoclet.generated=true 2>&1 | /bin/tee \$II/whytest" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm junitdoclet.generate" >> $ii/$fn
echo ":!cd $ant_dir;$ant_pgm $other_bld_parms clean" >> $ii/$fn
echo ":!\$I/massjupdate.sh $adir" >> $ii/$fn
echo ":!\$I/massupdate.sh \"$tag\" $adir" >> $ii/$fn
echo ":!\$I/mkallfiles.sh -f $fn" >> $ii/$fn
echo ":!\$I/mkallfiles.sh" >> $ii/$fn
echo ":e \$I/mkallfiles.sh" >> $ii/$fn
echo ":e \$I/mkcommlines.sh" >> $ii/$fn
echo ":e $ii2/ssalogs/debug/SSADebug.log" >> $ii/$fn
echo ":e $sdir/lib" >> $ii/$fn
echo ":e $sdir/target" >> $ii/$fn
echo ":e $sdir/odsEar/target" >> $ii/$fn
tmpa=$II/tmpa$$ #.wmf$' -e 'Root$' -e 'Tag$' -e '\.dfUseCase$' -e 'class'
#  -e '\.#' > $tmpa
/bin/find $sdir -type f \( ! -name '*.jar' -a ! -name '*.wmf' \
  -a ! -name '*Root' -a ! -name '*.dfPackage' -a ! -path '*/Repository' \
	-a ! -path '*/Entries' -a ! -path '*/Tag' -a ! -name '*.dfUseCase' \
	-a ! -path '*/CVS/*' -a ! -path '*/.svn/*' \
	-a ! -name '*.dfActivity' -a ! -name '*.dfSequence' \
	-a ! -name '*.class' \) > $tmpa
/bin/grep -v -e 'gif$' -e '/target/' -e '/eclipse-classes/' $tmpa > $tmpb
grep 'java$' $tmpb > $ii/$fn.cscope
/bin/sed -e 's/^/:e /' $tmpb >> $ii/$fn
echo ':1,$s/; *System.out.println("here.*$/;/' >> $ii/$fn
echo ':1,$s/; *m_midlet.recordMsg("here.*$/;/' >> $ii/$fn
echo '"End' >> $ii/$fn
/bin/sed -e 's/^/:e /' < $tmpa > $II/$fn.all
printf "#!/bin/sh\n:!rm $ii/whylog\n" > $ii/$fn.logcvs
/bin/grep -v -e 'gif$' -e '/target/' $tmpa | /bin/sed -e 's#^#:!$I/logcvs.sh #' >> $ii/$fn.logcvs
printf "#!/bin/sh\n:!rm $ii/whycot\n" > $ii/$fn.cotcvs
/bin/grep -v -e 'gif$' -e '/target/' $tmpa | /bin/sed -e 's#^#:!$I/cotcvs.sh #' >> $ii/$fn.cotcvs
/bin/mv $tmpa $II/$fn.tmp
}
cfn="$3"
if [[ (-z "$one_fn") || ("$one_fn" == "$cfn") ]] ; then
	common_lines "$3" "$4" "$5"
	lend_secs=$SECONDS
	echo \
  "$3\
 ELAPSED=$(( (lend_secs - lstart_secs)/60 )):\
$(( (lend_secs - lstart_secs)%60 ))"
fi
rm -f $tmpb
